/**
 * This file is part of JNotifyOSD.
 *
 * JNotifyOSD is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * JNotifyOSD is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with JNotifyOSD. If not, see <http://www.gnu.org/licenses/>.
 */

package notifyosd;

import java.io.File;
import java.lang.management.ManagementFactory;
import java.lang.ref.SoftReference;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

/**
 * Factory service for {@link NativeNotification} objects.
 * This class requires the native "jnotify" library to be loaded.
 * This means that either libjnotify.so (linux) or jnotify.dll (windows)
 * must be in the java.library.path.
 * @author Giulio Franco <g.franco9@studenti.unisa.it>
 * @ThreadSafe
 */
final class NativeNotificationService extends NotificationService {
    static {
        System.loadLibrary("jnotify");
    }
    
    private volatile Set<String> capabilities;
    private volatile ServerInformation info;
    private volatile String appName;
        
    NativeNotificationService() {
        appName = ManagementFactory.getRuntimeMXBean().getName();
        nativeInit(appName);
    }
        
    @Override
    protected void finalize() throws Throwable{
        nativeUninit();
        super.finalize();
    }
    
    /**
     * Returns a set of {@link String}s,
     * describing the capabilities of this service.
     * 
     * @return Set of capability strings.
     */
    public Set<String> getServerCaps() {
        /*
         * XXX if libnotify is thread safe,
         * race conditions here are not an issue.
         */
        if (capabilities == null) {
            String[] arrayCaps = nativeGetServerCaps();
            capabilities = Collections.unmodifiableSet(
                    new HashSet<String>(Arrays.asList(arrayCaps)));
        }
        
        return capabilities;
    }
    
    /**
     * Returns information about the notification server.
     * @return {@link ServerInformation} object
     */
    public ServerInformation getServerInfo() {
        /*
         * XXX if libnotify is thread safe,
         * race conditions here are not an issue.
         */
        if (info == null) {
            String[] data = nativeGetServerInfo();
            info = new ServerInformation(data[0], data[1], data[2], data[3]);
        }
        
        return info;
    }
    
    /**
     * Returns the application name
     * that will be used to communicate with the notification server.
     * @return Application name
     */
    public String applicationName() {
        if (appName == null) {
            appName = nativeGetAppName();
        }
        return appName;
    }
    
    /**
     * Creates a new NativeNotification object
     * @param title Title of the notification
     * @return New notification
     */
    public NativeNotification newNotification(String title) {
        return new NativeNotification(title, null, null);
    }
    
    /**
     * Creates a new NativeNotification object
     * @param title Title of the notification
     * @param body  Text of the notification
     * @return New notification
     */
    public NativeNotification newNotification(String title, String body) {
        return new NativeNotification(title, body, null);
    }
    
    /**
     * Creates a new NativeNotification object
     * @param title Title of the notification
     * @param body  Text of the notification
     * @param icon  Path of the icon that will be displayed in the notification
     * @return New notification
     */
    public NativeNotification newNotification(String title, String body, String icon) {
        return new NativeNotification(title, body, icon);
    }
    
    /**
     * Creates a new NativeNotification object
     * @param title Title of the notification
     * @param body  Text of the notification
     * @param icon  Icon that will be displayed in the notification
     * @return New notification
     */
    public NativeNotification newNotification(String title, String body, File icon) {
        return new NativeNotification(title, body, icon.getAbsolutePath());
    }
    
    /**
     * Creates a new NativeNotification object
     * @param title Title of the notification
     * @param icon  Icon that will be displayed in the notification
     * @return New notification
     */
    public NativeNotification newNotification(String title, File icon) {
        return new NativeNotification(title, null, icon.getAbsolutePath());
    }
    
    private native void nativeInit(String appName);
    private native void nativeUninit();
    private native boolean nativeIsInitted();
    private native String[] nativeGetServerCaps();
    private native String[] nativeGetServerInfo();
    private native String nativeGetAppName();
}
